import { getProduct } from "&/api"
export default {
    namespaced: true,
    state: {
        name: '产品Product',
        products: []
    },
    mutations: {
        //设置产品数据
        SET_PRODUCT(state, payload) {
            state.products = payload
        },
        //减少库存
        DECREMENT_INVENTORY(state, product_id) {
            // console.log(product_id)
            //通过product_id到products中找到对应的数据,让库存-1
            let target = state.products.find(item => item.id === product_id)

            // console.log(target)
            //让库存减少

            target.inventory--
        }
    },
    actions: {
        //获取产品数据
        async FETCH_PRODUCT(context, payload) {
            //通过api接口获取购物车数据
            let data = await getProduct()
            //提交给mutation
            context.commit('SET_PRODUCT', data)
        }
    },
    getters: {},
    modules: {}
}